let g = getApp().globalData
let util = require('./util.js')

function login(fn) {
    // 检查能否弹出授权框，能则弹出，不能则提示
    wx.checkSession({
        fail: res => {
            wxLogin()
        },
        success: res => {
            console.log('登录态未过期，不能弹出窗口-----------------------')
            if (wx.getSetting) {
                wx.getSetting({
                    success: (res) => {
                        if (res.authSetting['scope.userInfo']) {
                            // 已经登录状态不需要重新授权
                            wxLogin()
                        } else {
                            // 未登录状态，跳转到授权列表
                            wx.openSetting({
                                success(res) {
                                    console.log(res)
                                    if (res.authSetting['scope.userInfo']) {
                                        wxLogin()
                                    } else {
                                        fn(false)
                                    }
                                }
                            })
                        }
                    }
                })
            }

        }
    })

    /**微信的登录 */
    function wxLogin() {
        wx.login({
            success: (res) => {
                console.log('loginSuccess--------------', res)
                let code = res.code
                wx.getUserInfo({
                    //已授权
                    success: (res) => {
                        console.log('getUserInfo======================', res)
                        util.showLoading('登录中...')
                        getOwnUserInfo(code, res.iv, res.encryptedData)
                    },
                    //拒绝授权
                    fail: (res) => {
                        console.log('getUserInfoFail-=================', res)
                        fn(false)
                    }
                })
            }
        })
    }

    /**用户授权，向服务端获取用户数据 */
    function getOwnUserInfo(code, iv, encryptedData) {
        let url = `onLogin`
        let body = {
            code: code,
            iv: iv,
            encryptedData: encryptedData
        }
        util.httpPost({
            url: url,
            body: body,
            fn: res => {
                console.log('onlogin', res)
                try {
                    let userInfo = res.data.result
                    wx.setStorageSync('userInfo', userInfo)
                    wx.showToast({
                        title: '登录成功',
                    })
                    g.isLogined = true
                    g.userInfo = userInfo
                    console.log('login', g)
                    fn(true)
                } catch (e) { }
            },
            failFn: failRes => {
                util.hideLoading()
                wx.showToast({
                    title: failRes.data.msg,
                })
            }
        })
    }
}

function checkLogin(fn) {
    if (g.isLogined) {
        fn(true)
    } else {
        fn(false)
    }
}

module.exports = {
    login: login,
    checkLogin: checkLogin
}